#pragma once

#include <QObject>
#include <QThread>

namespace Util {

typedef std::function<int()> work_func_t;

class Work : public QObject
{
    Q_OBJECT
public:
    explicit Work(work_func_t workFunc)
    {
        m_workFunc = workFunc;
    }
    ~Work() {}
public slots:
    void doWork()
    {
        auto ret = m_workFunc();
        Q_EMIT resultReady(ret);
    }
signals:
    void resultReady(const int result);
private:
    work_func_t m_workFunc;
};

class CThreadHandle : public QObject
{
    Q_OBJECT
public:
    explicit CThreadHandle(work_func_t workFunc)
    {
        Work* worker = new Work(workFunc);
        worker->moveToThread(&workerThread);
        QObject::connect(&workerThread, &QThread::started, worker, &Work::doWork);
        QObject::connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
//        QObject::connect(worker, &Work::resultReady,this,&CHandle::handleResult);
        workerThread.start();
    }
    ~CThreadHandle()
    {
        workerThread.quit();
        workerThread.wait();
    }
//public slots:
//    void handleResult() {}
private:
    QThread workerThread;
};
};


